package cn.cytong.util;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-7
 * Time: 下午2:46
 * To change this template use File | Settings | File Templates.
 */
public class DataTypeTool {
    public static BigDecimal valueOf(String data){

        try {
            return new BigDecimal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    /**
     * 字符串是否为整型格式
     * @param value
     * @return
     */
    public static boolean isInteger(String value){
        return RegexTool.check(value, "^[-+]?[\\d]*$");
    }

    /**
     * 判断是否为浮点数，包括double和float
     * @param value
     * @return
     */
    public static boolean isDouble(String value) {
        return RegexTool.check(value, "^[-+]?[.\\d]*$");

    }

    /**
     * 求百分百，20% 返回 20
     * @param num1
     * @param num2
     * @return
     */
    public static int percent(int num1, int num2){
        float num3 = (float)num1/(float)num2;
        num3 = num3*100;
        int percent = (int)num3;
        return percent;
    }

    public static Long[] toArray(Set<Long> set){
        Long[] arr = new Long[]{};

        if(set != null){
            int i = 0;
            arr = new Long[set.size()];
            for ( Iterator<Long> iterator = set.iterator(); iterator.hasNext(); ) {
                Long next = iterator.next();
                arr[i] = next;
                i++;
            }
        }

        return arr;
    }

    public static String toString(Set<Long> set){
        String result = "";

        if(set != null){
            int i = 0;

            for ( Iterator<Long> iterator = set.iterator(); iterator.hasNext(); ) {
                if(i>0){
                    result += ",";
                }
                result += iterator.next().toString();

                i++;
            }
        }

        return result;
    }

    public static String toString(Long[] arr) {
        String result = "";

        if (arr != null) {
            for (int i = 0; i < arr.length; i++) {
                if (i > 0) {
                    result += ",";
                }

                result += arr[i].toString();
            }
        }

        return result;
    }

    public static Long[] toArray(String data){
        return toArray(data, ",");
    }

    public static Long[] toArray(String data, String part){
        if(StringTool.isEmpty(data)){
            return new Long[]{};
        }
        String[] arr = data.split(part);

        Long[] result = new Long[arr.length];
        for (int i = 0; i < arr.length; i++) {
            String str = arr[i];
            result[i] = Long.parseLong(str);
        }

        return result;
    }

    /**
     * 字符串转换成集合
     * @param data 例："23,321,33"
     * @return
     */
    public static Set<Long> toSet(String data){
        return toSet(data, ",");
    }

    public static Set<Long> toSet(String data, String part){
        Set<Long> result = new HashSet<Long>();

        if(StringTool.isNotEmpty(data)){
            String[] arr = data.split(part);
            for (int i = 0; i < arr.length; i++) {
                String s = arr[i].trim();
                Long e = Long.parseLong(s);
                result.add(e);
            }
        }

        return result;
    }

    public static void main(String[] args) {
        Boolean b = DataTypeTool.isDouble("10.0");
        System.out.println(b.toString());
    }
}
